import pymysql
import hashlib
import smtplib
import time
import random
import string
from email.mime.text import MIMEText
from tkinter import messagebox


# 连接 MySQL 数据库
class Mysql_c():
    def __init__(self,sql = None,value=None):
        self.conn = pymysql.connect(
            host='127.0.0.1',  # 主机名
            port=3306,  # 端口号，MySQL默认为3306
            user='root',  # 用户名
            password='xiaofeng321',  # 密码
            database='wms',)
        self.cursor = self.conn.cursor()
        self.realdata = self.cursor.execute(sql,value)

    def fetchal(self):
        result = self.cursor.fetchall()
        self.result = [dict(zip([k[0] for k in self.cursor.description], row)) for row in result]
        return self.result

    def cursor_1(self):
        return self.realdata

    def commit(self):
        self.conn.commit()

    def close_conn(self):
        try:
            if self.conn:
                self.conn.close()
        except pymysql.Error as e:
            print("Error: %s" % e)

class Md5():
    def __init__(self,pwd = None):
        md5 = hashlib.md5(b"xx")
        md5.update(pwd.encode("utf-8"))
        self.str_md5 = md5.hexdigest()

    def strmd5(self):
        return self.str_md5




class Smtplib():
    def __init__(self,theme = None,malibox = None,value = None):
        sender = user = '3372769229@qq.com'  # 发送方的邮箱账号
        passwd = "eaezjklamqnuciaj"  # 授权码

        receiver = malibox # 接收方的邮箱账号，不一定是QQ邮箱

        # 纯文本内容
        if theme == "注册":
            msg = MIMEText(f'验证码:{value} \n您正在使用HKIWMS系统的{theme}功能,如非本人操作,\n请及时联系我,{user},请勿把验证码透露给他人！', 'html', 'utf-8')
        elif theme == "找回密码":
            msg = MIMEText(f'验证码:{value} \n您正在使用HKIWMS系统的{theme}功能,如非本人操作,\n请及时联系我,{user},请勿把验证码透露给他人！', 'html','utf-8')
        # From 的内容是有要求的，前面的abc为自己定义的 nickname，如果是ASCII格式，则可以直接写
        msg['From'] = f'HIKWMS <{user}>'
        msg['To'] = receiver
        msg['Subject'] = f'HIKWMS账号{theme}验证'  # 点开详情后的标题

        try:
            # 建立 SMTP 、SSL 的连接，连接发送方的邮箱服务器
            smtp = smtplib.SMTP_SSL('smtp.qq.com', 465)

            # 登录发送方的邮箱账号
            smtp.login(user, passwd)

            # 发送邮件 发送方，接收方，发送的内容
            smtp.sendmail(sender, receiver, msg.as_string())


            smtp.quit()
        except Exception as e:
            messagebox.showerror(title="警告", message=f"邮件发送失败{e}")


class Random():
    def code(self):
        num = string.digits
        num_digits = ""
        for i in range(6):
            num1 = random.choice(num)
            num_digits = num_digits + num1
        return num_digits


if __name__ == "__main__":
    # print(Mysql_c('SELECT * FROM b_user').cursor_1())
    # print(Mysql_c(f'''select * from b_user where username = "admin123"''').cursor_1().real)
    # Smtplib(theme="注册",malibox= "xiaofeng321520@163.com",value="wertw")
    sql = f"""insert into b_user(username,passwd,mailbox) values("aaa","xiaofeng321","xiaofee@163.com")"""
    sql_data = Mysql_c(sql=sql)
    sql_data.commit()


# "eaezjklamqnuciaj"